import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:luckiness/utils/icon.dart';
import 'package:toggle_switch/toggle_switch.dart';
import 'package:transformer_page_view/transformer_page_view.dart';
import '../utils/general.dart';
import '../utils/webview.dart';
import '../components/ffbtn.dart';
import '../components/ffrow.dart';
import '../components/ffswitch.dart';
typedef ClickBlock = Function(int index);
class Home extends StatelessWidget {
  final VoidCallback onPress;
  Home({this.onPress});

  /// banner
  Widget bannerWidget(count, ClickBlock block) {
    List<String> list = [
      'assets/images/home/swiper1.jpg',
      'assets/images/home/swiper2.jpg',
      'assets/images/home/swiper3.jpg'
    ];
    return Swiper(
      itemCount: count,
      itemBuilder: (BuildContext context, int index){
        return Image.asset(list[index], fit:BoxFit.fill);
      },
      onTap: (index){
        block(index);
      },
      autoplay: true,
      pagination: SwiperPagination(),
    );
  }
  /// 地址
  Widget addressWidget(BuildContext context, String address, double distance) {

    double scale = widthScale(context);

    return Container(
      color: Colors.white,
      child: Column(
        children:<Widget>[

          Padding(
            padding: EdgeInsets.fromLTRB(16.0, 13.0 * scale, 16.0, 18.0 * scale),
            child: Row(
              mainAxisAlignment:MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: <Widget>[
                Column(
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: <Widget>[
                    Text(address, style: TextStyle(fontSize:15 * scale, color:Color.fromRGBO(56, 56, 56, 1.0)),),
                    Text('距离 $distance m', style: TextStyle(fontSize:11 * scale, color:Color.fromRGBO(128, 128, 128, 1.0)),),
                  ],
                ),
                FFSwitch(open:'自提', close:'外送', onChange: (bl, str)=>print('你选择了$str'),),
                // ToggleSwitch(
                //   activeBgColor: Color.fromRGBO(136, 175, 213, 1.0), 
                //   activeTextColor: Colors.white, 
                //   inactiveBgColor: Colors.white, 
                //   inactiveTextColor: Color.fromRGBO(136, 175, 213, 1.0), 
                //   labels: ['自提', '外送'],
                //   minWidth: 90,
                //   cornerRadius: 10,
                // )
              ],
            ),
          ),
          Container(
            color: separatorLineColor,
            padding: EdgeInsets.symmetric(horizontal:16.0),
            height: 1,
          )

        ]
      ),
    );
  }
  /// cell
  Widget listCell(BuildContext context, String title, String subtitle, Widget icon) {
    double scale = widthScale(context);
    return GestureDetector(
      onTap:(){
        if (title == "现在下单") {
          onPress();
        }
      },
      child: Container(
        color:Colors.white,
        child: Column(
          children:<Widget>[
            Padding(
              padding: EdgeInsets.fromLTRB(16.0, 15.0 * scale, 16.0, 15.0 * scale),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(title, style:TextStyle(fontSize:16 * scale, color:Color.fromRGBO(56, 56, 56, 1.0), fontWeight:FontWeight.bold), textAlign: TextAlign.left,),
                      Text(subtitle, style:TextStyle(fontSize:11 * scale, color:Color.fromRGBO(128, 128, 128, 1.0))),
                    ],
                  ),
                  icon,
                ],
              ),
            ),

            Container(
              color: separatorLineColor,
              padding: EdgeInsets.fromLTRB(16.0, 0, 16.0, 0),
              height: 1,
            )
          ],
        ),
      ),
    );
  }
  
  @override
  Widget build(BuildContext context) {
    // return bannerWidget(3, context);
    double hei = 200 * widthScale(context);

    return SingleChildScrollView(
      child:Container(
        color: Colors.white,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[

            Stack(
              children:[
                SizedBox(
                  height:hei,
                  child:bannerWidget(3, (index){
                    Navigator.of(context).push(PageRouteBuilder(pageBuilder: (context, ani, ani2){
                      return Webview(title:'用户协议');
                    }));
                  }),
                ),
                Container(
                  padding: EdgeInsets.fromLTRB(screenWidth(context) - 65, statusBarHeight(context), 15, 0),
                  child: FFBtn(
                    icon: icontupian4(), 
                    bgcolor: Color.fromARGB(77, 0, 0, 0), 
                    onPress: ()=>print('点击扫描'),
                  ),
                )
              ]            
            ),

            addressWidget(context, '青年公寓', 88),

            listCell(context, '现在下单', 'ORDER NOW', FFBtn(icon:icontupian3(size:22), isBorder: true,)),
            listCell(context, '咖啡钱包', 'COFFRR WALLET', FFBtn(icon:icontupian(size:22), isBorder:true)),
            listCell(context, '送Ta咖啡', 'SEND COFFEE', FFBtn(icon:icontupian1(size:22), isBorder:true)),
            listCell(context, '企业账户', 'ENTERPRISE ACCOUNT', FFBtn(icon:iconorder(size:22), isBorder:true)),
 
            Padding(
              padding: EdgeInsets.fromLTRB(16.0, 0, 16.0, 0),
              child:Image.asset('assets/images/home/bottom_bar.png', height: 55 * widthScale(context)),
            )
          ],
        ),
      )
    );
  }
}